package com.saver.view.splash;

import java.util.List;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.saver.R;
import com.saver.core.business.TransaccionBusiness;
import com.saver.core.domain.Banco;
import com.saver.core.domain.Parametro;
import com.saver.core.persistence.DBManager;
import com.saver.core.persistence.MensajeTextoInbox;
import com.saver.core.persistence.ParametroSQLite;
import com.saver.core.processor.ProcessSMSFactory;
import com.saver.core.processor.async.CompletoProcesoSMS;
import com.saver.core.processor.async.GUIProcesadorSMS;
import com.saver.core.util.Constantes;
import com.saver.view.MainActivity;
import com.saver.view.TerminosCondicionesDialog;

/**
 * Created by HeRGoS on 29/03/2015
 */
public class SplashScreen extends Activity implements CompletoProcesoSMS , TerminosCondicionesDialog.TerminosCondiconesListener {

	private static final String TAG = "SplashScreen";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        PackageManager managerv = this.getPackageManager();
        PackageInfo info;
		try {
			info = managerv.getPackageInfo(this.getPackageName(), 0);
			TextView version = (TextView) this.findViewById(R.id.versionMingy);
	        version.setText("versi\u00F3n "+info.versionName);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
        
        ParametroSQLite parametroSQLite = new ParametroSQLite(this);
        Integer flagAceptoTerminosCondiciones = Integer.parseInt(parametroSQLite.buscarPorPropiedad(Constantes.ACEPTO_TERMINOS_CONDICONES).getValor());
        if (flagAceptoTerminosCondiciones == 0){
            FragmentManager manager = getFragmentManager();
            TerminosCondicionesDialog dialog = new TerminosCondicionesDialog();
            dialog.show(manager, null);
        }else{
        	cartarHistorico();
        }
	            
    }
    
    public void cartarHistorico(){
    	DBManager.initialize(this);
        int cantidad = DBManager.transaccionSQLite().numeroTransacciones();
        Log.d(TAG, "cantidad: " + cantidad);
        if (cantidad == 0) {
            TransaccionBusiness transaccionBusiness = new TransaccionBusiness(this, new MensajeTextoInbox(this));
            List<Banco> bancos = DBManager.bancoSQLite().buscarTodoActivo();
            ProcessSMSFactory processSMSFactory = new ProcessSMSFactory(bancos);
            GUIProcesadorSMS guiProcesadorSMS = new GUIProcesadorSMS(transaccionBusiness, processSMSFactory, 
            		bancos, this, Constantes.TRANSACCION_REVISADA);
            guiProcesadorSMS.execute();
        } else {
        	new LoadSMSHistorico().execute();
        }
    }
    
    private void cargarPrincipal () {
    	Log.d(TAG, "cargarPrincipal");
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

	@Override
	public void completo(Object object) {
		cargarPrincipal ();
	}

	@Override
	public void exception(Exception ex) {
		Log.e(TAG, "Error al cargar el historico", ex);
	}

    private class LoadSMSHistorico extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
        	try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				Log.e(TAG, "error en la interrupion", e);
			}
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            cargarPrincipal ();
        }
    }

	@Override
	public void onDialogPositiveClick(DialogFragment dialog) {
		ParametroSQLite parametroSQLite = new ParametroSQLite(this);
    	Parametro parametro = new Parametro();
    	parametro.setPropiedad(Constantes.ACEPTO_TERMINOS_CONDICONES);
    	parametro.setValor("1");
    	parametroSQLite.actualizar(parametro);
		cartarHistorico();
	}

	@Override
	public void onDialogNegativeClick(DialogFragment dialog) {
		finish();
	}
}
